package cn.xxm.strategy.pay.i18n.response;

import cn.xxm.strategy.pay.i18n.utils.FI18nUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class FFRuntimeException extends RuntimeException {

	private static final long serialVersionUID = -2116971365138188651L;

	private String code;
	private String msg;

	public FFRuntimeException(String code) {
		super(Errcode.code2MsgMap.get(code));
		this.code = code;
	}

	public FFRuntimeException(String code, String message) {
		super(message);
		this.code = code;
	}

	/**
	 * 国际化错误码异常
	 * @param responseInfo
	 * @param <E>
	 */
	public <E extends Enum<?> & ResponseInfo> FFRuntimeException(E responseInfo) {
		setCodeAndMsg(responseInfo);
	}
	@JsonIgnore
	public <E extends Enum<?> & ResponseInfo> void setCodeAndMsg(E responseInfo) {
		this.code = responseInfo.getCode();
		this.msg = getI18nMsg(responseInfo);
	}
	/**
	 * 获取国际化响应信息
	 */
	public static String getI18nMsg(ResponseInfo responseInfo) {
		String oriMsg = responseInfo.getMsg();
		String i18nKey = responseInfo.getClass().getSimpleName().replace("ResponseInfo", "").toLowerCase() + "."
				+ oriMsg;
		String msg = FI18nUtil.getI18nValue(i18nKey);
		System.out.println("msg:"+msg);
		return msg == null ? oriMsg : msg;
	}


	public FFRuntimeException(String code, String message, Throwable throwable) {
		super(message, throwable);
		this.code = code;
	}

	public FFRuntimeException(Exception e) {
		super(e);
	}

	public String getCode() {
		return code;
	}

}
